package cn.com.elite.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;
import java.net.URLConnection;
import java.text.*;
import java.util.*;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


public class Util {
	private static final Log log=LogFactory.getLog(Util.class);
    public static final String DateSeparator = "-";

          /** */
          public static final String LongDatePattern = "yyyy" + DateSeparator + "MM" +
              DateSeparator + "dd HH:mm:ss";

          /** */
          public static final String ShortDatePattern = "yyyy" + DateSeparator + "MM" +
              DateSeparator + "dd";

  public Util() {
  }
  

	public static String get_interface_return(String address) throws Exception{
		String returnxml="";
		URL url=new URL(address);
		URLConnection conn=url.openConnection();
		conn.setConnectTimeout(30000);
		conn.setReadTimeout(30000);
		BufferedReader rd=new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8")); 
		returnxml=rd.readLine(); 
		return returnxml;
	}


	public static String getERRO(Exception e){
		String str="";
		try{
            StringWriter sw = new StringWriter();  
            e.printStackTrace(new PrintWriter(sw, true));  
            str = sw.toString(); 	
		}catch(Exception e1){
			str=e.toString();
			log.error("getERRO:"+e1.toString());
		}
		return str;
	}
  public static String filter(String str) {
    if (str == null)
      str = "";
    if (str.equals("null"))
      str = "";
    return str.trim();
  }
  public static String filter1(String str) {
	    if (str == null)
	      str = "0";
	    if (str.equals("null"))
	      str = "0";
	    return str.trim();
	  }

  public static String filterForObject(Object Obj) {
    String str = "";
    if (Obj == null)
      str = "";
    else
      str = filter(Obj.toString());
    return str;
  }

  public static String filterToValue(String str, String str_value) {
    str = filter(str);
    if (str.equals(""))
      str = str_value;
    return str;
  }

  public static int filterToInteger(String str) {
    str = filterToValue(str, "0");
    return Integer.parseInt(str.trim());
  }

  public static int filterIntergerToInt(Integer inte) {
    int i_str = 0;
    if (inte == null)
      i_str = 0;
    else
      i_str = inte.intValue();
    return i_str;
  }

  /**
   * ���ָ����ʽ��String���͵�����
   * @param date
   * @param type
   * @return
   */
  public static String formatDate(java.util.Date date, String format) {
    String result = "";
    if (date != null) {
      try {
        format = filterToValue(format, "yyyy-MM-dd");
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        result = formatter.format(date);
      }
      catch (Exception e) {
      }
    }
    return result;
  }

  /**
             *
             * @param datetime Date
             * @param pattern String
             * @return String
             */
            public static String DateToStr(Date datetime, String pattern) {
              return DateToStr(datetime, pattern, null);
            }

            /**
             * format the date object to a string
             * @param datetime the date object.
             * @param pattern the string pattern
             * @param locale the locale.if the locale is null ,the locale will be set to the default.
             * @return string
             * @author ford. added in 2002/10/16
             * */
            public static String DateToStr(Date datetime, String pattern, Locale locale) {
              String strdate = "";
              if (datetime == null) {
                return "";
              }
              if (locale == null)
                locale = Locale.getDefault();
              SimpleDateFormat simformat = new SimpleDateFormat(pattern, locale);
              strdate = simformat.format(datetime);
              return strdate;
	  }
  public static String formatDate(java.util.Date date) {
    String result = "";
    String format = "";
    if (date != null) {
      try {
        format = filterToValue(format, "yyMMdd");
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        result = formatter.format(date);
      }
      catch (Exception e) {
      }
    }
    return result;
  }


  /**
   * ��ȡ��ǰʱ��
   * @return
   */
  public static java.util.Date getCurDate() {
    Calendar cal = Calendar.getInstance(TimeZone.getDefault());
    return cal.getTime();
  }

  /**
   * ��������
   * @param digits λ��
   * @return
   */
  public static String genRandom(int digits) {
    int random = (int) (Math.random() * digits);
    String randomstr = String.valueOf(random);
    return randomstr;
  }

}
